package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.Student;
import cc.mrbird.web.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class StudentController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(StudentController.class);

	@Autowired
	private StudentService studentService;

	@Value("${sys.admin}")
	private String admin;

	@Log("获取考生信息")
	@RequestMapping("student")
	@RequiresPermissions("student:list")
	public String index() {
		return "web/student/student";
	}

	@RequestMapping("student/list")
	@ResponseBody
	public Map<String, Object> studentList(QueryRequest request, Student student,String sonUserName) {
		List<Student> list;
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		if(student.getUserName().equalsIgnoreCase(admin) && sonUserName == null){
			student.setUserName("");
			list = this.studentService.findStudentByCondition(student);
		}else if(student.getUserName().equalsIgnoreCase(admin) && sonUserName != null){
			student.setUserName(sonUserName);
			list = this.studentService.findStudentByCondition(student);
		}else {
			list = this.studentService.findStudentByCondition(student);
		}
		PageInfo<Student> pageInfo = new PageInfo<Student>(list);
		return getDataTable(pageInfo);
	}

	@RequestMapping("student/getStudent")
	@ResponseBody
	public ResponseBo getUser(String studentId) {
		try {
			Student student = this.studentService.findById(studentId);
			return ResponseBo.ok(student);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取用户信息失败，请联系网站管理员！");
		}
	}

	@Log("新增考生信息")
	@RequiresPermissions("student:add")
	@RequestMapping("student/add")
	@ResponseBody
	public ResponseBo addStudent(Student student,String userName) {
		try {
			this.studentService.addStudent(student,userName);
			return ResponseBo.ok("新增考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增考生失败，请联系网站管理员！");
		}
	}

	@Log("修改考生信息")
	@RequiresPermissions("student:update")
	@RequestMapping("student/update")
	@ResponseBody
	public ResponseBo updateStudent(Student student) {
		try {
			this.studentService.updateStudent(student);
			return ResponseBo.ok("修改考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改考生失败，请联系网站管理员！");
		}
	}

	@Log("删除考生")
	@RequiresPermissions("student:delete")
	@RequestMapping("student/delete")
	@ResponseBody
	public ResponseBo deleteStudents(String ids) {
		try {
			this.studentService.deleteStudents(ids);
			return ResponseBo.ok("删除考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除考生失败，请联系网站管理员！");
		}
	}

	@RequestMapping("student/checkStudentId")
	@ResponseBody
	public boolean checkStudentId(String studentId, String oldstudentId) {
		if (StringUtils.isNotBlank(oldstudentId) && studentId.equalsIgnoreCase(oldstudentId)) {
			return true;
		}
		Student result = this.studentService.findById(studentId);
		if (result != null)
			return false;
		return true;
	}

}
